package com.sfdk.admin.helpers.spritesheetEditor
{
	import com.sfdk.admin.data.RectangleEx;
	import com.sfdk.admin.data.spritesheetEditor.TilePiece;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.collections.ArrayCollection;

	public class SpritesheetCuttingHelper
	{
		public static function findAllShape(bitmapData : BitmapData, existingData : ArrayCollection = null) : ArrayCollection
		{
			var tilePieces:ArrayCollection;
			if(existingData == null)
				tilePieces = new ArrayCollection();
			else
				tilePieces = existingData;
			var posX : int;
			var posY : int;
			var imageWidth : int = bitmapData.width;
			var imageHeight : int = bitmapData.height;
			for(posY  = 0; posY < imageHeight; posY++)
			{
				for(posX = 0; posX < imageWidth; posX++)
				{
					var pixel : uint;
					pixel = bitmapData.getPixel32(posX, posY);
					// If we click on blank spot we end now
					if(pixel >> 24 != 0)
					{
						// We need to check that our position is not included in existing image
						var tilePiece : TilePiece;
						var alreadyFound : Boolean = false;
						var idx : int;
						var len : int = tilePieces.length;
						for(idx = 0; idx < len && !alreadyFound; idx++)
						{
							tilePiece = tilePieces[idx];
							if(tilePiece.sourceRect.contains(posX, posY))
								alreadyFound = true;
						}
						if(!alreadyFound)
						{
							tilePiece = findShape(bitmapData, posX, posY);
                            if(tilePiece)
							    tilePieces.addItem(tilePiece);
						}
					}
				}
			}
			return tilePieces;
		}
			
		public static function findShape(bitmapData : BitmapData, posX : int, posY : int) : TilePiece
		{
			var tmpPosX : int = posX;
			var tmpPosY : int = posY;
			
			var notFoundLeft : Boolean = true;
			var notFoundTop : Boolean = true;
			var notFoundBottom : Boolean = true;
			var notFoundRight : Boolean = true;
			var topBorder : int;
			var bottomBorder : int;
			var leftBorder : int;
			var rightBorder : int;
			var pixel : uint;
			
			var maxWidth : int = bitmapData.width;
			var maxHeight : int = bitmapData.height;
			var noConflict : Boolean;
			pixel = bitmapData.getPixel32(posX, posY);
			// If we click on blank spot we end now
			if(pixel >> 24 == 0)
				return null;
			
			while(tmpPosX > 0 && notFoundLeft)
			{
				pixel = bitmapData.getPixel32(tmpPosX, posY);
				if(pixel >> 24 == 0)
				{
					notFoundLeft = false;
					leftBorder = tmpPosX;
				}
				else
					tmpPosX--;
			}
			tmpPosX = posX;
			while(tmpPosX < maxWidth && notFoundRight)
			{
				pixel = bitmapData.getPixel32(tmpPosX, posY);
				if(pixel >> 24 == 0)
				{
					notFoundRight = false;
					rightBorder = tmpPosX;
				}
				else
					tmpPosX++;
			}
			
			while(tmpPosY > 0 && notFoundTop)
			{
				pixel = bitmapData.getPixel32(posX, tmpPosY);
				if(pixel >> 24 == 0)
				{
					notFoundTop = false;
					topBorder = tmpPosY;
				}
				else
					tmpPosY--;
			}
			tmpPosY = posY;
			while(tmpPosY < maxHeight && notFoundBottom)
			{
				pixel = bitmapData.getPixel32(posX, tmpPosY);
				if(pixel >> 24 == 0)
				{
					notFoundBottom = false;
					bottomBorder = tmpPosY;
				}
				else
					tmpPosY++;
			}
			
			noConflict = false;
			while(!noConflict)
			{
				noConflict = true;
				// validate Top
				tmpPosX = leftBorder;
				
				while(tmpPosX <= rightBorder)
				{
					pixel = bitmapData.getPixel32(tmpPosX, topBorder);
					if(pixel >> 24 == 0)
					{
						tmpPosX++;
					}
					else
					{
						noConflict = false;
						topBorder--;
						tmpPosX = leftBorder
					}		
				}
				
				// validate Right
				tmpPosY = topBorder + 1;
				while(tmpPosY <= bottomBorder)
				{
					pixel = bitmapData.getPixel32(rightBorder, tmpPosY);
					if(pixel >> 24 == 0)
					{
						tmpPosY++;
					}
					else
					{
						noConflict = false;
						rightBorder++;
						tmpPosY = topBorder + 1;
					}		
				}
				
				// validate Bottom
				tmpPosX = rightBorder - 1;
				while(tmpPosX >= leftBorder)
				{
					pixel = bitmapData.getPixel32(tmpPosX, bottomBorder);
					if(pixel >> 24 == 0)
					{
						tmpPosX--;
					}
					else
					{
						noConflict = false;
						bottomBorder++;
						tmpPosX = rightBorder - 1;
					}		
				}
				
				// validate Left
				tmpPosY = bottomBorder - 1;
				while(tmpPosY > topBorder)
				{
					pixel = bitmapData.getPixel32(leftBorder, tmpPosY);
					if(pixel >> 24 == 0)
					{
						tmpPosY--;
					}
					else
					{
						noConflict = false;
						leftBorder--;
						tmpPosY = bottomBorder - 1;
					}		
				}
			}
			
			leftBorder++;
			topBorder++;

            if((rightBorder - leftBorder) > 0 && (bottomBorder - topBorder) > 0)
            {
                var foundBitmapData : BitmapData = new BitmapData(rightBorder - leftBorder, bottomBorder - topBorder);
                foundBitmapData.copyPixels(bitmapData, new Rectangle(leftBorder, topBorder, rightBorder - leftBorder, bottomBorder - topBorder), new Point());

                var foundTilePiece : TilePiece = new TilePiece(bitmapData);
                foundTilePiece.init(foundBitmapData, new RectangleEx(leftBorder, topBorder, rightBorder - leftBorder, bottomBorder - topBorder));

    			return foundTilePiece;
            }
            return null;
		}
	}
	
	
}